//
//  ResultView.m
//  checkApp
//
//  Created by ma on 2017/6/14.
//  Copyright © 2017年 ma. All rights reserved.
//

#import "ResultView.h"

@implementation ResultView
{
    UILabel *orderLabel;
    UILabel *buyLabel;
    UILabel *checkLabel;
    UILabel *timeLabel;
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self _initView];
    }
    return self;
}

- (void)_initView {
    orderLabel = [UILabel labelWithFrame:CGRectZero text:@"订单号：9412312" font:13.0*boringScale textColor:color(0, 0, 0, 0.8)];
    [self addSubview:orderLabel];
    [orderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10*boringScale);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30*boringScale);
    }];
    
    buyLabel = [UILabel labelWithFrame:CGRectZero text:@"购买数量：3" font:13.0*boringScale textColor:color(0, 0, 0, 0.8)];
    [self addSubview:buyLabel];
    [buyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(orderLabel.mas_bottom).offset(10*boringScale);
        make.trailing.mas_equalTo(self.mas_centerX).offset(-10*boringScale);
        make.height.mas_equalTo(30*boringScale);
    }];
    
    checkLabel = [UILabel labelWithFrame:CGRectZero text:@"验证数量：2" font:13.0*boringScale textColor:color(0, 0, 0, 0.8)];
    [self addSubview:checkLabel];
    [checkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(orderLabel.mas_bottom).offset(10*boringScale);
        make.leading.mas_equalTo(self.mas_centerX).offset(10*boringScale);
        make.height.mas_equalTo(30*boringScale);
    }];
    
    UILabel *checkTime = [UILabel labelWithFrame:CGRectZero text:@"验证时间" font:15.0*boringScale textColor:color(0, 0, 0, 0.8)];
    [self addSubview:checkTime];
    [checkTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(checkLabel.mas_bottom).offset(40*boringScale);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(30*boringScale);
    }];
    
    timeLabel = [UILabel labelWithFrame:CGRectZero text:@"" font:13.0*boringScale textColor:[UIColor grayColor]];
    timeLabel.numberOfLines = 0;
    [self addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(checkTime.mas_bottom).offset(10*boringScale);
        make.centerX.mas_equalTo(self);
        make.height.mas_greaterThanOrEqualTo(30*boringScale);
        make.bottom.mas_equalTo(-30*boringScale);
    }];
}

- (void)setCheckModel:(CheckCodeModel *)checkModel {
    if (_checkModel != checkModel) {
        _checkModel = checkModel;
    }
    orderLabel.text = [NSString stringWithFormat:@"订单号：%@",_checkModel.orderSn];
    buyLabel.text = [NSString stringWithFormat:@"购买数量：%@",_checkModel.buy];
    checkLabel.text = [NSString stringWithFormat:@"已验证数量：%@",_checkModel.checked];
    
    NSArray *timeArr = _checkModel.usedTime;
    NSMutableString *mStr = [NSMutableString string];
    for (NSString *str in timeArr) {
        NSString *newStr = [MYFactoryManager getStartTimeDate:[str floatValue] format:@"YYYY.MM.dd    HH : mm"];
        [mStr appendString:[NSString stringWithFormat:@"%@\n",newStr]];
    }
    timeLabel.attributedText = [self getAttributedStringWithString:mStr lineSpace:8*boringScale];
}

-(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 调整行间距
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    return attributedString;
}

@end
